package cn.dujc.superkid.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.core.util.TextColorBuilder;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.CourseCatalogueAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.CourseEntity;
import cn.dujc.superkid.bean.MyClassResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.VideoListResult;
import cn.dujc.superkid.utils.Preferences;

public class LessonDetailActivity extends BaseListActivity {
    public static final String LESSON_COURSE = "LESSON_COURSE";
    private final String  TAG  = LessonDetailActivity.class.getName();
    private final List<VideoListResult.VideoEntity> List = new ArrayList<>();
    private final List<Object> mList = new ArrayList<>();
    private MyClassResult.MyClass myClasslesson;
    private VideoListResult.VideoEntity  nullVideoEntity;
    private View mHeaderView;
    private TextView mTvTitle;
    private TextView mTvDesc;
    private TextView mTvInfo;
    private CourseCatalogueAdapter mAdapter;
    private String class_title;
    private String class_id;

    @Override
    public void initBasic(Bundle savedInstanceState) {
//        Intent intent = getIntent();
//        class_title = intent.getStringExtra("class_title");
//        class_id = intent.getStringExtra("class_id");
//        Log.i(TAG,"==============caizhijia1======LessonDetailActivity=====" +class_id);
        myClasslesson = extras().get(LESSON_COURSE);
        super.initBasic(savedInstanceState);
        if (myClasslesson != null) {
            setTitle(myClasslesson.getClass_title());
        }
        loadData();

    }

    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        mAdapter = new CourseCatalogueAdapter(mList);
        mAdapter.addHeaderView(initHeader(null));
        return mAdapter;
    }

    @Override
    protected void onItemClick(int position) {
        final Object item = mAdapter.getItem(position);
        if (item instanceof VideoListResult.VideoEntity) {//视频
//            starter().go(CourseVideoActivity.class);
            Intent in = new Intent();
            in.setClass(mActivity, CourseVideoActivity.class);
            in.putExtra("video_id", List.get(position).getId());
            in.putExtra("course_id",myClasslesson.getClass_id() );
            in.putExtra("is_learn", List.get(position).getIs_learn());
            in.putExtra("videoTitle",List.get(position).getVideo_title());
            mActivity.startActivity(in);

        } else if (item instanceof VideoListResult.ExaminationEntity) {//考试
            starter().go(CourseExamActivity.class);
        }
    }

    @Override
    protected void loadMore() {
        refreshDone();
        loadDone(true, true);
    }

    @Override
    protected void reload() {
        loadData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Preferences.loginSuccess(requestCode, resultCode)) {
            reload();
        }
    }

    private View initHeader(VideoListResult videoInfo) {
        if (mHeaderView == null) {
            mHeaderView = getLayoutInflater().inflate(R.layout.header_course_detail, null);
            mTvTitle = mHeaderView.findViewById(R.id.tv_title);
            mTvDesc = mHeaderView.findViewById(R.id.tv_desc);
            mTvInfo = mHeaderView.findViewById(R.id.tv_info);
        }
        if (videoInfo != null) {
            mTvTitle.setText(videoInfo.getTitle());
            mTvDesc.setText(videoInfo.getIntroduce());
            int color = ContextCompat.getColor(mActivity, R.color.colorAccent);
            mTvInfo.setText(new TextColorBuilder()
                    .addTextPart("累计")
                    .addTextPart(color, videoInfo.getStudy_num())
                    .addTextPart("位同学参加了学习，课程总共")
                    .addTextPart(color, videoInfo.getCourse_num())
                    .addTextPart("节")
                    .build());
        }
        return mHeaderView;
    }

    private void loadData() {
        showRefreshing();
        if (myClasslesson == null) {
            loadFailure();
            ToastUtil.showToast(mActivity, "参数错误");
            return;
        }
        if (!Preferences.isLogin(this)) {
            return;
        }
//        Log.i(TAG,"==============caizhijia1======LessonDetailActivity=====" +class_id);
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getCourse().videoList(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel", Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("course_id",myClasslesson.getClass_id() )
                .put("logincode", loginResult.getLogincode())
                //.put("video_id",)
                .create()).enqueue(new ResultCallBack<Result<VideoListResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<VideoListResult> body) {
                mList.clear();
                if (body != null) {
                    final VideoListResult data = body.getData();
                    if (data != null) {
                        initHeader(data);
                        final List<VideoListResult.VideoEntity> video = data.getVideo();
                        List.add(nullVideoEntity);
                        List.addAll(video);
                        if (video != null && video.size() > 0) {
                            mList.add("课程目录");
                            mList.addAll(video);
                        }
                        final List<VideoListResult.ExaminationEntity> examination = data.getExamination();
                        if (examination != null && examination.size() > 0) {
                            mList.add("测试卷");
                            mList.addAll(examination);
                        }
                    }
                }
                notifyDataSetChanged(true);
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                super.onFailureCallback(code, msg);
                loadFailure();
            }
        });
    }
}
